package org.orz.cloud.auth.utils;

import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletRequest;

public class NetUtils {
    private NetUtils() {
    }

    public static String getRemoteIp(HttpServletRequest request) {
        if (null == request) {
            return "";
        } else {
            String ip = request.getHeader("X-Real-IP");
            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("x-forwarded-for");
            }

            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            return ip;
        }
    }

    public static String getUserAgent(HttpServletRequest request) {
        if (null == request) {
            return "";
        } else {
            String userAgent = request.getHeader("user-agent");
            return StrUtil.isBlank(userAgent) ? "" : userAgent;
        }
    }

    public static String getPlatform(HttpServletRequest request) {
        if (null == request) {
            return "";
        } else {
            String platform = request.getHeader("platform");
            return StrUtil.isBlank(platform) ? "web" : platform;
        }
    }
}
